package sg.nus.iss.vmcs.controllers;

import sg.nus.iss.vmcs.VMCSException;
import sg.nus.iss.vmcs.gui.SimulatorControlPanel;

/**
 * Main Controller class for the VMCS Simulation <br><br>
 * Modifications:
 * <li>Philip - 26/12/2010, created the machinery controller and transaction controller<\li>
 * <br><br>
 * @author Pang Pingli
 * @author Philip
 */
public class MainController {
	
	/** Simulation controller controlling the Simulator Control Panel */
    private SimulationController simulatorCtrl;
    
    /** Machinery controller controlling the Machinery Control Panel */
    private MachineryController	machineryCtrl;
    
    /** Maintenance Controller controlling the  Maintenance Panel */
    private MaintenanceController maintenanceCtrl;
    
    /** Transaction controller controlling the Customer Panel */
    private	TransactionController transactionCtrl;
    
    /** Store controller controlling store related events */
    private StoreController storeCtrl;
    
    /**
     * Creates an instance of the MainController and initializes its contents
     * @throws VMCSException
     */
    public MainController(){}
    
    /**
     * Initializes the MainController and all subcontrollers managed by it.
     * @throws VMCSException
     */
    private void initialize() {
		//Create the simulator controller
		simulatorCtrl 	= new SimulationController(this);
		
		//Create and Initialize the machinery controller
		machineryCtrl = new MachineryController(this);
		machineryCtrl.initialize();
		
		//Create the maintenance controller
		maintenanceCtrl = new MaintenanceController(this);
		
		//Create the transaction controller
		transactionCtrl = new TransactionController(this);
		
		//Create and Initialize the Store Controller
    	storeCtrl = new StoreController(this);
    }
    
    /**
     * Retrieves the reference to the Simulator Controller Panel
     * @return SimulatorControlPanel Frame acting as the Simulator Control Panel
     */
    public SimulatorControlPanel getSimulatorControlPanel(){
    	return simulatorCtrl.getSimulatorControlPanel();
    }

    /**
     * Retrieves the reference to the Simulation Controller, the controller for the Simulator Control Panel
     * @return SimulationController
     */
    public SimulationController getSimulationController(){
    	return simulatorCtrl;
    }

    /**
     * Retreieves a reference to the Store Controller, used for managing store related items
     * @return StoreController
     */
    public StoreController getStoreController(){
    	return storeCtrl;
    }

    /**
     * Retreieves a reference to the Maintenance Controller, used for managing the Maintenance panel
     * @return MaintenanceController
     */
    public MaintenanceController getMaintenanceController() {
    	return maintenanceCtrl;
    }
    
    /**
     * Retreieves a reference to the Maintenance Controller, used for managing the Machinery panel
     * @return MachineryController
     */
    public MachineryController getMachineryController() {
    	return machineryCtrl;
    }
    
    /**
     * Retreieves a reference to the Maintenance Controller, used for managing the Customer panel
     * @return TransactionController
     */
    public TransactionController getTransactionController() {
    	return transactionCtrl;
    }

    /**
     * Starts the simulation by setting the Start Simulation button on the UI to disabled, and 
     * enabling all the other buttons
     */
    public void start() throws VMCSException{
    	initialize();
		simulatorCtrl.displaySimulatorControlPanel();
		simulatorCtrl.setSimulationActive(false);
    }
    
    /**
     * Closes down the entire simulation
     */
    public void closeDown(){
    	//If Maintenance Controller has not closed down, do not allow the system to shut down
    	if (maintenanceCtrl.closeDown() == false) return;
    	
    	try {
    		storeCtrl.closeDown();
    	} 
    	catch (VMCSException e){
    		System.out.println("MainController.closeDown"+ e);
    	}
    	finally {
    		simulatorCtrl.closeDown();
    	}
    }
}
